package com.weharvest.edu.handler;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import com.weharvest.edu.exception.EduException;
import com.weharvest.edu.exception.WechatException;
import com.weharvest.edu.vo.ResultVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class EduExceptionHandler {

    /**
     * 拦截所有抛出系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = EduException.class)
    @ResponseBody
    public ResultVo handlerException(EduException e){
        ResultVo resultVo=new ResultVo();
        resultVo.setMsg(e.getMessage());
        resultVo.setCode(e.getCode());
        return resultVo;
    }


    /**
     *
     */

    @ExceptionHandler(value = WechatException.class)
    @ResponseBody
    public ResultVo WechatException(WechatException e){
        ResultVo resultVo=new ResultVo();
        resultVo.setMsg("你已经登录过了");
        resultVo.setCode(406);
        return resultVo;
    }
    /**
     * 拦截所有抛出未知异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVo mysqlException(Exception e){
        ResultVo resultVo=new ResultVo();
        resultVo.setMsg("网络异常,请重新刷新!");
        resultVo.setCode(405);
        e.printStackTrace();
        return resultVo;
    }
}
